<template>
  <PageContainer>
    <view class="container">
      <Header title="业务结果" />
      <TResult
        :types="getApprovalStatusText.type"
        :title="getApprovalStatusText.title"
        :content="getApprovalStatusText.content"
        :cancel-text="getApprovalStatusText.cancelText"
        :confirm-text="getApprovalStatusText.confirmText"
        @cancel="cancel"
        @confirm="confirm"
      />
      <AttachPopup
        v-model:visible="attachPopupVisible"
        :attache-vo-list="resultData?.headquartersMemberAttacheVos"
      />
      <MoreConnections
        v-if="resultData?.activityShortDetailVo.activityId"
        :activityId="resultData?.activityShortDetailVo.activityId"
        :scene="3"
      ></MoreConnections>
      <FixedCode />
    </view>
  </PageContainer>
</template>

<script lang="ts" setup>
  /* activityResult 报名结果 */
  import PageContainer from '@/components/PageContainer/index.vue';
  import Header from '@/components/Header/index.vue';
  import TResult, { type TResultProps } from '@/components/TResult/index.vue';
  import FixedCode from '@/components/OfficialAccount/fixedCode.vue';
  import { navigateBack, onLoad, reLaunch } from '@/router';
  import { uniongetOrderCode } from '@/api/activity';
  import { computed, ref, unref } from 'vue';
  import { onPullDownRefresh } from '@dcloudio/uni-app';
  import { ActivityRegisterStateEnum, type UniongetOrderCodeResult } from '@/api/activity/types';
  import AttachPopup from '../components/AttachPopup.vue';
  import { useUserStore } from '@/store/modules/user';
  import MoreConnections from '../components/MoreConnections/MoreConnections.vue';

  const attachPopupVisible = ref(false);
  const equipmentNumber = ref<string>();

  const { setUserInfo } = useUserStore();

  setUserInfo();

  const cancel = () => {
    if (getApprovalStatusText.value.cancelText === '报名详情') {
      toOrderDetail();
      return;
    }
    attachPopupVisible.value = true;
  };

  function confirm() {
    if (getApprovalStatusText.value.confirmText === '立即签到') {
      if (resultData.value?.activityRegisterState === ActivityRegisterStateEnum.REGISTER_PASS) {
        reLaunch({
          name: 'signInPage',
          options: {
            scene: resultData.value?.activityShortDetailVo.activityId!,
            equipmentNumber: unref(equipmentNumber) as string,
          },
        });
      } else {
        uni.scanCode({
          success: (res) => uni.reLaunch({ url: '/' + res.path }),
        });
      }
      return;
    }

    if (getApprovalStatusText.value.confirmText === '刷新结果') {
      __init(resultData.value?.orderCode);
      return;
    }

    toOrderDetail();
  }

  function toOrderDetail() {
    if (resultData.value?.orderCode) {
      reLaunch({
        name: 'activityRegisterOrder',
        options: {
          orderCode: resultData.value?.orderCode,
          scene: 'createActivityRegisterOrder',
        },
      });
    } else {
      navigateBack();
    }
  }

  const resultData = ref<UniongetOrderCodeResult>();
  const getApprovalStatusText = computed(() => {
    const obj = {
      type: '' as TResultProps['types'],
      title: '',
      content: '',
      confirmText: '',
      cancelText: '',
    };
    switch (resultData.value?.activityRegisterState) {
      case ActivityRegisterStateEnum.REGISTER_GENERATE:
        obj.type = 'waiting';
        obj.title = '报名生成中';
        obj.content = '请耐心等待报名生成';
        obj.cancelText = '报名详情';
        obj.confirmText = '刷新结果';
        break;
      case ActivityRegisterStateEnum.REGISTER_WAIT:
        obj.type = 'waiting';
        obj.title = '报名审核中';
        obj.content = '请耐心等待活动主办方审核';
        obj.cancelText = '报名详情';
        obj.confirmText = '刷新结果';
        break;
      case ActivityRegisterStateEnum.REGISTER_FAIL:
        obj.type = 'error';
        obj.title = '报名失败';
        obj.content = '很抱歉，失败原因请查看报名详情';
        obj.confirmText = '报名详情';
        obj.cancelText = '联系活动客服';
        break;
      default:
        obj.title = '报名成功';
        obj.content = '可以到报名详情查看具体情况~';
        obj.type = 'success';
        if (resultData.value?.activityShortDetailVo.isSign) {
          obj.confirmText = '立即签到';
          if (resultData.value?.activityRegisterState === ActivityRegisterStateEnum.REGISTER_PASS) {
            uni.showModal({
              title: '签到提醒',
              content: '您已报名成功, 是否要立即签到',
              cancelText: '取消',
              cancelColor: '#999996',
              confirmText: '立即签到',
              confirmColor: '#2878ff',
              success: (res) => {
                if (res.confirm) {
                  confirm();
                }
              },
            });
          }
        }
        obj.cancelText = '报名详情';
        break;
    }

    return obj;
  });

  onLoad<'activityResult'>(async (op) => {
    equipmentNumber.value = op.equipmentNumber;
    __init(op.orderCode);
  });

  async function __init(orderCode?: string) {
    if (orderCode) {
      const res = await uniongetOrderCode(orderCode);
      resultData.value = res;
    }
    uni.stopPullDownRefresh();
  }

  onPullDownRefresh(() => {
    __init(resultData.value?.orderCode);
  });
</script>

<style lang="scss" scoped></style>
